import java.util.*;

/**
 * @author LKQ
 * @date 2022/4/11 15:16
 * @description 双指针，模拟上升和下降
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] arr = {7,4,8};
        solution.longestMountain(arr);
    }
    public int longestMountain(int[] arr) {
        int n = arr.length;
        int l = 0, r = 0, up = 0, down = 0;
        boolean appearSame = false;
        int ans = 0;
        while (r < n) {
            // 如果超出边界，那么就直接跳出
            if (r + 1 >= n) {
                break;
            }
            // 上升
            while (r + 1 < n && arr[r + 1] > arr[r]) {
                r++;
                up = 1;
            }
            // 水平移动
            while (r + 1 < n && arr[r + 1] == arr[r]) {
                r++;
                appearSame = true;
            }
            if (appearSame) {
                l = r;
                up = down = 0;
                appearSame = false;
                continue;
            }
            // 下降
            while (r + 1 < n && arr[r + 1] < arr[r]) {
                r++;
                down = 1;
            }
            if (up == 1 && down == 1) {
                // 经历了先升后降的过程
                ans = Math.max(ans, r - l + 1);
            }
            // 经过一轮后，单升 或 单降 或 先降后升 标志都重置
            up = down = 0;
            l = r;
        }
        return ans;
    }
}
